﻿using Common;
using Linguist.Acoustic.Tiedstate;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Api
{
    /// <summary>
    /// Base class for high-level speech recognizers.
    /// </summary>
    public class AbstractSpeechRecognizer
    {
        protected Context context;
        protected Recognizer.Recognizer recognizer;
    
        protected ClusteredDensityFileData clusters;

        protected SpeechSourceProvider speechSourceProvider;

        /// <summary>
        /// Constructs recognizer object using provided configuration.
        /// </summary>
        /// <param name="configuration"></param>
        public AbstractSpeechRecognizer(Configuration configuration)
            :this(new Context(configuration))
        {

        }

        protected AbstractSpeechRecognizer(Context context) 
        {
            this.context = context;
            recognizer = (Recognizer.Recognizer)context.getInstance(typeof(Recognizer.Recognizer));
            speechSourceProvider = new SpeechSourceProvider();
        }

        /// <summary>
        /// Returns result of the recognition.
        /// </summary>
        /// <returns></returns>
        public SpeechResult getResult() 
        {
            IResult result = recognizer.recognize();
            return null == result ? null : new SpeechResult(result);
        }
    
        public Stats createStats(int numClasses) 
        {
            clusters = new ClusteredDensityFileData(context.getLoader(), numClasses);
            return new Stats(context.getLoader(), clusters);
        }

        public void setTransform(ITransform transform) 
        {
            if (clusters != null) {
                context.getLoader().update(transform, clusters);
            }
        }

        public void loadTransform(String path, int numClass)
        {
    	    clusters = new ClusteredDensityFileData(context.getLoader(), numClass);
    	    Transform transform = new Transform((Sphinx3Loader)context.getLoader(), numClass);
    	    transform.load(path);
    	    context.getLoader().update((ITransform)transform, clusters);
        }
    }
}
